package cn.iocoder.yudao.module.scrm.service.marketing.visitor;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.visitor.MarketingVisitorPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.visitor.MarketingVisitorSaveReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.marketing.MarketingVisitorDO;
import cn.iocoder.yudao.module.scrm.dal.mysql.marketing.MarketingVisitorMapper;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.Collections;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.scrm.enums.ErrorCodeConstants.MARKETING_VISITOR_NOT_EXISTS;

/**
 * 营销访客数据 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class MarketingVisitorServiceImpl implements MarketingVisitorService {

    @Resource
    private MarketingVisitorMapper marketingVisitorMapper;

    @Resource
    private PermissionApi permissionApi;

    @Override
    public Integer createMarketingVisitor(MarketingVisitorSaveReqVO createReqVO) {
        // 插入
        MarketingVisitorDO marketingVisitor = BeanUtils.toBean(createReqVO, MarketingVisitorDO.class);
        marketingVisitorMapper.insert(marketingVisitor);
        // 返回
        return marketingVisitor.getId();
    }

    @Override
    public void updateMarketingVisitor(MarketingVisitorSaveReqVO updateReqVO) {
        // 校验存在
        validateMarketingVisitorExists(updateReqVO.getId());
        // 更新
        MarketingVisitorDO updateObj = BeanUtils.toBean(updateReqVO, MarketingVisitorDO.class);
        marketingVisitorMapper.updateById(updateObj);
    }

    @Override
    public void deleteMarketingVisitor(Integer id) {
        // 校验存在
        validateMarketingVisitorExists(id);
        // 删除
        marketingVisitorMapper.deleteById(id);
    }

    private void validateMarketingVisitorExists(Integer id) {
        if (marketingVisitorMapper.selectById(id) == null) {
            throw exception(MARKETING_VISITOR_NOT_EXISTS);
        }
    }

    @Override
    public MarketingVisitorDO getMarketingVisitor(Integer id) {
        return marketingVisitorMapper.selectById(id);
    }

    @Override
    public PageResult<MarketingVisitorDO> getMarketingVisitorPage(MarketingVisitorPageReqVO pageReqVO) {
        if(!permissionApi.roleIsSuperAdmin(getLoginUserId()).getCheckedData()) {
            //用户id转成企业微信wxId进行查询
            pageReqVO.setCreator(Objects.requireNonNull(getLoginUserId()).toString());
        }
        return marketingVisitorMapper.selectPage(pageReqVO);
    }

}